package top.haijunit.work.core.doamin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import lombok.Data;

import java.io.Serializable;

/**
 * 【说明类功能】
 *
 * @author zhanghaijun
 * @since 2024-08-27
 */
@Data
@Schema(description = "分页查询")
public class PageQuery implements Serializable {

    @Schema(description = "当前页数", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    @Min(value = 1, message = "页码最小值为 1")
    private Long page = 1L;

    @Schema(description = "分页大小", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
    @Min(value = 1, message = "每页显示最小值为 1")
    private Long size = 10L;

    public long offset() {
        return (Math.max(1, getPage()) - 1) * Math.max(1, getSize());
    }

    public <T> Page<T> build() {
        return new Page<>(Math.max(1, getPage()), Math.max(1, getSize()));
    }
}
